package com.chengxiaoxiao.ai_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengxiaoxiao.ai_demo.entity.InstructionInfo;
import com.chengxiaoxiao.ai_demo.exception.GlobalException;
import com.chengxiaoxiao.ai_demo.mapper.InstructionInfoMapper;
import com.chengxiaoxiao.ai_demo.service.InstructionInfoService;
import com.chengxiaoxiao.ai_demo.vo.CodeMsg;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * instructionInfo 服务实现类
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2025-03-18 10:02:00
 */
@Service
public class InstructionInfoServiceImpl extends ServiceImpl<InstructionInfoMapper, InstructionInfo> implements InstructionInfoService {

    /**
     * 指令匹配正则
     */
    public static final Pattern INSTRUCTION_PATTERN = Pattern.compile("/(\\S*)\\s");

    /**
     * 获取instructionInfo详情
     *
     * @param id ID
     * @return 单个instructionInfo详情
     */
    @Override
    public InstructionInfo detailById(String id) {
        return getById(id);
    }

    /**
     * 增加instructionInfo
     *
     * @param instructionInfo instructionInfo对象
     * @return 增加后的数据
     */
    @Override
    public InstructionInfo saveInstructionInfo(InstructionInfo instructionInfo) {
        //指令名称不能重复
        if (this.getByName(instructionInfo.getInstructionName()) != null) {
            throw new GlobalException(CodeMsg.INSTRUCTION_NOT_FOUND);
        }
        this.baseMapper.insert(instructionInfo);
        return instructionInfo;
    }

    /**
     * 根据指令名称查询指令信息
     *
     * @param instructionName 指令名称
     * @return 指令信息
     */
    public InstructionInfo getByName(String instructionName) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<InstructionInfo>().eq(InstructionInfo::getInstructionName, instructionName));
    }

    /**
     * 修改instructionInfo
     *
     * @param instructionInfo instructionInfo对象
     * @return 修改后的数据
     */
    @Override
    public InstructionInfo updateInstructionInfoById(InstructionInfo instructionInfo) {
        //指令名称不能重复
        InstructionInfo oldInstruction = this.getByName(instructionInfo.getInstructionName());
        if (oldInstruction != null && !oldInstruction.getId().equals(instructionInfo.getId())) {
            throw new GlobalException(CodeMsg.INSTRUCTION_NOT_FOUND);
        }

        this.baseMapper.updateById(instructionInfo);
        return instructionInfo;
    }

    /**
     * 根据ID删除
     *
     * @param id ID
     */
    @Override
    public void deleteById(String id) {
        removeById(id);
    }

    /**
     * 根据文本构建用户消息
     *
     * @param message 原始消息
     * @return 用户消息
     */
    @Override
    public String buildMessage(String message) {
        Matcher matcher = INSTRUCTION_PATTERN.matcher(message);
        if (matcher.find()) {
            String instructionName = matcher.group(1).trim();
            InstructionInfo instructionInfo = this.getByName(instructionName);
            if (instructionInfo == null) {
                return message;
            }
            return message.replaceAll(matcher.group(0), instructionInfo.getInstructionMessage());
        }
        return message;
    }
}